package com.itheima.util;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.Product;

import java.util.ArrayList;
import java.util.List;

public class MyDatabase {
    //集合当做数据库
    private static List<Product> list = new ArrayList<>();

    //初始化数据
    static {
        list.add(new Product("苹果", 12, "中国"));
        list.add(new Product("香蕉", 14, "美国"));
        list.add(new Product("橘子", 15, "泰国"));
    }

    //定义方法，用来获取数据库集合
    public static List getData() {
        return list;
    }


    public List<Product> findAll() {
        return list;
    }

    public void add(Product product) {
        list.add(product);
    }

    public void delete(Integer id) {
        if (CollUtil.isNotEmpty(list)) {
            for (Product product : list) {
                if (id.equals(product.getId())) {
                    list.remove(product);
                    return;
                }
            }
        }
    }
}
